package com.tfjybj.itoo.exam.provider.controller;

import org.apache.dubbo.config.annotation.Reference;;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.StudentEntity;
import com.tfjybj.itoo.exam.model.StudentModel;
import com.tfjybj.itoo.exam.provider.service.StudentService;
import com.tfjybj.itoo.exam.template.ScreenClassStu;
import com.tfjybj.itoo.teach.FacadeApi.StudentApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * StudentController
 * student表
 *
 * @author 杨江涛
 * @version 2.0.0
 * @since 2.0.0 2018-11-15 10:35:56
 */
@Api(tags = {"student表接口"})
@RequestMapping(value = "/student")
@RestController
public class StudentController {
    /**
     * 定义打印日志相关
     */
    private static final Logger logger = LoggerFactory.getLogger(StudentController.class);

    @Resource
    private StudentService studentService;

    //贾建清-2019年6月1日09:12:16
    @Reference(check = false)
    private StudentApi studentApi;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model StudentModel
     * @return 添加的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody StudentModel model) {
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())) {
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        com.tfjybj.itoo.teach.entity.StudentEntity studentEntity = new com.tfjybj.itoo.teach.entity.StudentEntity();
        BeanUtils.copyProperties(model, studentEntity);
        studentApi.save(studentEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        studentApi.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        studentApi.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model StudentModel
     * @return 修改后的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id修改student")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody StudentModel model) {
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())) {
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        com.tfjybj.itoo.teach.entity.StudentEntity studentEntity = new com.tfjybj.itoo.teach.entity.StudentEntity();
        BeanUtils.copyProperties(model, studentEntity);
        studentApi.updateById(studentEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找Student
     *
     * @param id 主键id
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        com.tfjybj.itoo.teach.entity.StudentEntity studentEntity = studentApi.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", studentEntity);
    }

    /**
     * 分页查询所有Student
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "分页查询所有Student")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<com.tfjybj.itoo.teach.entity.StudentEntity> students = studentApi.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", students);
    }

    /**
     * 根据姓名模糊查询student
     *
     * @param name     姓名
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的student
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据姓名模糊查询student", notes = "分页根据姓名模糊查询student")
    @GetMapping(value = "queryStudentByLikeName/{pageNo}/{pageSize}")
    public ItooResult queryStudentByLikeName(@RequestParam(required = false, defaultValue = "") String name,
                                             @ApiParam(value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                             @ApiParam(value = "记录数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<StudentEntity> studentList = studentService.queryByLikeName(name, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", studentList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 根据学号查询student所有信息
     *
     * @param code 姓名
     * @return 根据学号查询学生所有信息
     * @author 杨江涛
     * @since 2.0.0 2018-11-19 15:09:05
     */
    @ApiOperation(value = "根据学号查询学生所有信息")
    @GetMapping(value = {"/findByCode/{code}"})
    public ItooResult findByCode(@ApiParam(value = "学号code", required = true) @PathVariable String code) {
        if (StringUtils.isEmpty(code)) {
            return ItooResult.build(ItooResult.FAIL, "学号为空！");
        }
        try {
            List<StudentModel> stuList = studentService.findByCode(code.trim());
            return ItooResult.build(ItooResult.SUCCESS, "查询成功", stuList);
        } catch (Exception e) {
            return ItooResult.build(ItooResult.FAIL, "信息查询失败！");
        }
    }


    /**
     * 根据学生Id查询学生信息
     *
     * @param data 数据
     * @return 根据学生Id查询学生信息
     * @author 樊建华
     * @since 2018年11月22日11:29:43
     */

    @ApiOperation(value = "根据学生Id查询学生信息", notes = "输入data来分页查询")
    @RequestMapping(value = {"/findStudentById/{data}"}, method = RequestMethod.GET)
    @ResponseBody
    public ItooResult findStudentById(
            @ApiParam(value = "数据", name = "data", required = true) @PathVariable String data) {
        try {
            StudentModel studentMode = studentService.findStudentById(data);
            return ItooResult.build("0000", "查询学生成功！", studentMode);
        } catch (Exception e) {
            logger.error("远程调用失败！", e);
            return ItooResult.build("1111", "查询学生失败！", e);
        }
    }

    /**
     * 根据行政班id查询对应的所有学生信息
     *
     * @param classId 行政班id
     * @return 根据行政班id查询对应的所有学生信息
     * @author 杨江涛
     * @since 2018-11-28 20:23:10
     */
    @ApiOperation(value = "根据行政班id查询对应的所有学生信息")
    @GetMapping(value = {"/findByClassId/{classId}"})
    public ItooResult findByClassId(@ApiParam(value = "行政班id", required = true) @PathVariable String classId) {
        List<StudentModel> studentModelList = studentService.findByClassId(classId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", studentModelList);
    }

    /**
     * 根据学号查询学生id
     *
     * @param codeId 学号
     * @return 学生id
     * @author 郭满亮
     * @since 2019年2月28日10:19:46
     */
    @ApiOperation(value = "根据学号查询学生id")
    @PostMapping(value = {"/getCodeById/{codeId}"})
    public ItooResult getCodeById(@ApiParam(value = "学号", required = true) @PathVariable String codeId) {
        return studentService.getCodeById(codeId);
    }

    /**
     * 根据上课班id查询该班所有学生
     *
     * @param teachClassId 上课班id
     * @return 学生集合
     * @author 郭满亮
     * @since 2019年2月27日16:44:34
     */
    @ApiOperation(value = "根据上课班id查询该班所有学生")
    @PostMapping(value = {"/getStudent/{teachClassId}"})
    public ItooResult getStudent(@ApiParam(value = "上课班id", required = true) @PathVariable String teachClassId) {
        List<StudentEntity> studentEntitieList = studentService.getStudent(teachClassId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", studentEntitieList);
    }

    /**
     *
     *
     * @param classId   班级id
     * @param startTime 考试开始时间
     * @author 马莹
     * @since 2018-12-7 20:02:20
     */
    /**
     * 根据班级id和考试开始时间查询考试时间为未冲突的学生
     *
     * @param classId             班级id
     * @param startTime           考试开始时间
     * @param endTime             考试结束时间
     * @param screeningPopulation 筛选人数数值
     * @param stuNameOrStuCode    模糊查询学生模糊值
     * @return List<ScreenClassStu>
     * @author 马莹
     * @since 2019年3月22日21:03:08
     */
    @ApiOperation(value = "根据行政班id查询对应的所有学生信息")
    @GetMapping(value = {"/queryNoExamination/{classId}/{startTime}/{endTime}"})
    public ItooResult queryNoExamination(@ApiParam(value = "行政班id", required = true)
                                         @PathVariable String classId, @PathVariable String startTime
            , @PathVariable String endTime,
                                         @RequestParam(required = false, defaultValue = "") Integer screeningPopulation,
                                         @RequestParam(required = false, defaultValue = "") String stuNameOrStuCode) {
        List<ScreenClassStu> studentModelList = studentService.queryNoExamination(classId, startTime, stuNameOrStuCode, endTime);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", studentModelList);
    }

    /**
     * 根据姓名、学号、班级名称模糊查询对应的学生信息及班级名称
     *
     * @param searchInfo 行政班id
     * @return 对应的学生信息及班级名称
     * @author 杨江涛
     * @since 2018-12-8 11:07:40
     */
    @ApiOperation(value = "根据姓名、学号、班级名称模糊查询对应的学生信息及班级名称")
    @GetMapping(value = {"/findClassStudent/{searchInfo}"})
    public ItooResult findClassStudent(@ApiParam(value = "姓名、学号、班级名称", required = true) @PathVariable String searchInfo) {
        List<StudentModel> studentModelList = studentService.findClassStudent(searchInfo);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", studentModelList);
    }
}    
